package cn.hbwe.controller;


import cn.hbwe.Dto.pLoginDTO;
import cn.hbwe.Dto.uLoginDTO;
import cn.hbwe.pojo.entity.Patient;
import cn.hbwe.pojo.entity.User;
import cn.hbwe.service.IUserService;
import cn.hbwe.util.Result;
import cn.hbwe.util.constant.BaseContext;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 7组
 * @since 2023-10-18
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService iUserService;

    /**
     * 用户登录
     * @param loginDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("/ulogin")
    @ApiOperation("用户登录")
    public Result<User> login(@RequestBody @Valid uLoginDTO loginDTO, BindingResult bindingResult) {
        List<ObjectError> errors = bindingResult.getAllErrors();
        for (ObjectError error : errors) {
            return Result.error(error.getDefaultMessage());
        }
       User user = iUserService.ulogin(loginDTO);
        Integer id = user.getUserId();
        BaseContext.setCurrentId(id);
        return Result.success(user);
    }




}
